package com.asman.worker.rn_ui;

import com.asman.worker.widgets.gsyVideoPlayer.EmptyControlVideo;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import org.jetbrains.annotations.NotNull;

import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

// JS中的使用：：：：
//
// <NativeLiveVideoPlayer
//    ref='NativeLiveVideoPlayer'
//    url={"http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4"}
//    onPlayStatusChanged={event => {
//        // 使用event.nativeEvent.msg获取原生层传递的数据
//        // -1:播放失败，0：当前是暂停状态，1：当前是播放状态
//        console.log("----+++++" + event.nativeEvent.status);
//    }}
//    style={{ height: 300 }} />

/**
 * @author ybk
 * @date :2019-10-15 10:44
 * @desc:
 */
public class NativeLiveVideoPlayer extends SimpleViewManager<EmptyControlVideo> {

    public static final String REACT_CLASS = "NativeLiveVideoPlayer";

    @Override
    public @Nonnull
    String getName() {
        return REACT_CLASS;
    }

    @Nonnull
    @Override
    protected EmptyControlVideo createViewInstance(@Nonnull ThemedReactContext reactContext) {
        EmptyControlVideo view = new EmptyControlVideo(reactContext.getCurrentActivity() == null ? reactContext : reactContext.getCurrentActivity());
        view.setOnPlayStatusListener(new EmptyControlVideo.OnPlayStatusListener() {
            @Override
            public void error() {
                callJSChangeStatus(reactContext, view, -1);
            }

            @Override
            public void play() {
                callJSChangeStatus(reactContext, view, 1);
            }

            @Override
            public void pause() {
                callJSChangeStatus(reactContext, view, 0);
            }
        });
        return view;
    }

    @ReactProp(name = "url", defaultDouble = 0)
    public void setUrl(EmptyControlVideo view, String url) {
        view.setUp(url, false, "");
    }

    /**
     * @param reactContext ThemedReactContext
     * @param view         EmptyControlVideo
     * @param playStatus   -1:播放失败，0：暂停中，1：播放中
     */
    private void callJSChangeStatus(@Nonnull ThemedReactContext reactContext, EmptyControlVideo view, int playStatus) {
        // 创建数据传递信使，类似于Android中的Bundle
        WritableMap data = Arguments.createMap();
        data.putString("status", String.valueOf(playStatus));// key用于在RN中获取传递的数据
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                view.getId(), // RN层原生层根据id绑定在一起
                EVENT_NAME_ONCLICK, // 事件名称
                data // 传递的数据
        );
    }

    @Override
    public void updateProperties(@Nonnull EmptyControlVideo viewToUpdate, ReactStylesDiffMap props) {
        super.updateProperties(viewToUpdate, props);
    }

    @javax.annotation.Nullable
    @Override
    public Map getExportedCustomDirectEventTypeConstants() {
        return MapBuilder.of(EVENT_NAME_ONCLICK, MapBuilder.of("registrationName", EVENT_NAME_ONCLICK));
    }

    //Native 暂停和播放 通知 EN
    private static final String EVENT_NAME_ONCLICK = "onPlayStatusChanged";

    //接受RN 的消息
    private static final String HANDLE_START_PLAY_NAME = "startPlay"; // 开始播放
    private static final String HANDLE_PAUSE_PLAY_NAME = "pausePlay"; // 暂停播放
    private static final String HANDLE_DESTROY_NAME = "destroy"; //销毁

    private static final int HANDLE_START_PLAY_ID = 1; // 开始播放
    private static final int HANDLE_PAUSE_PLAY_ID = 2; //暂停播放
    private static final int HANDLE_DESTROY_ID = 3; //销毁

    /**
     * 接收交互通知
     */
    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        return MapBuilder.of(
                HANDLE_START_PLAY_NAME, HANDLE_START_PLAY_ID,
                HANDLE_PAUSE_PLAY_NAME, HANDLE_PAUSE_PLAY_ID,
                HANDLE_DESTROY_NAME, HANDLE_DESTROY_ID
        );
    }

    /**
     * 根据命令ID，处理对应任务
     *
     * @param video
     * @param commandId
     * @param args
     */
    @Override
    public void receiveCommand(@NotNull EmptyControlVideo video, int commandId, @Nullable ReadableArray args) {
        switch (commandId) {
            case HANDLE_START_PLAY_ID://开始
                if (args != null) {
                    video.startPlay();
                }
                break;
            case HANDLE_PAUSE_PLAY_ID://暂停
                if (args != null) {
                    video.pausePlay();
                }
                break;
            case HANDLE_DESTROY_ID://销毁
                if (args != null) {
                    video.release();
                }
                break;
            default:
                break;
        }
    }
}